package com.hinnotek.warehouse.ui

import android.os.Build
import android.os.Bundle
import android.webkit.WebChromeClient
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.BuildConfig
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.databinding.ActivityWebBinding
import com.hinnotek.warehouse.viewmodel.WebViewModel
import com.lee.common.base.BaseActivity
import com.lee.common.ext.getArg
import com.lee.common.utils.ACTIVITY_WEB
import com.lee.common.utils.Skipping
import dagger.hilt.android.AndroidEntryPoint


@Route(path = ACTIVITY_WEB)
@AndroidEntryPoint
class WebActivity : BaseActivity<ActivityWebBinding>() {

    val link by getArg<String>("link")

    private val viewModel by viewModels<WebViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web)
        setTitle("详情")
        bindingView.vm = viewModel
        bindingView.executePendingBindings()
    }

    override fun initVew() {
        initWebView()
    }

    override fun setData() {

    }

    private fun initWebView() {
        bindingView.webView.run {
            settings.run {
                javaScriptEnabled = true
            }
            setBackgroundColor(0)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                WebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG)
            }
            webViewClient = object : WebViewClient() {
                override fun shouldInterceptRequest(
                    view: WebView,
                    url: String
                ): WebResourceResponse? {
                    return super.shouldInterceptRequest(view, url)
                }

                override fun shouldOverrideUrlLoading(
                    view: WebView,
                    url: String
                ): Boolean {
                    val isHttp = url.startsWith("http://") || url.startsWith("https://")
                    return if (isHttp) {
                        Skipping.navWeb(url, view.context)
                        true
                    } else {
                        true
                    }
                    return super.shouldOverrideUrlLoading(view, url)
                }

                override fun onPageFinished(view: WebView?, url: String?) {
                    super.onPageFinished(view, url)
                    imgReset()
                    viewModel.loaded.value = true
                }
            }
            webChromeClient = object : WebChromeClient() {
                override fun onReceivedTitle(view: WebView?, title: String) {
                    super.onReceivedTitle(view, title)
                    setTitle(title)
                }
            }
            loadUrl(link ?: "")
        }
    }

    /**
     * 对图片进行重置大小，宽度就是手机屏幕宽度，高度根据宽度比便自动缩放
     **/
    private fun imgReset() {
        bindingView.webView.loadUrl("javascript:(function(){" +
                "var objs = document.getElementsByTagName('img'); " +
                "for(var i=0;i<objs.length;i++) " +
                "{"
                + "var img = objs[i]; " +
                " img.style.maxWidth = '100%'; img.style.height = 'auto'; " +
                "}" +
                "})()");
    }


}